package at.fhooe.mcm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import at.fhooe.mcm.logic.core.Core;
import at.fhooe.mcm.logic.network.client.Client;

public class ConnectionView extends JFrame
{
	private static final long	serialVersionUID	= 133065657582934605L;
	
	private JTextField			m_portField;
	private JTextField			m_ipField;
	private Core				m_core;

	/**
	 * Create the frame
	 */
	public ConnectionView(Core _core)
	{
		super();
		m_core = _core;
		getContentPane().setLayout(null);
		setBounds(100, 100, 356, 133);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JLabel addressLabel = new JLabel();
		addressLabel.setText("Address:");
		addressLabel.setBounds(10, 10, 66, 16);
		getContentPane().add(addressLabel);

		m_ipField = new JTextField();
		m_ipField.setBounds(82, 8, 250, 20);
		getContentPane().add(m_ipField);

		final JLabel portLabel = new JLabel();
		portLabel.setText("Port:");
		portLabel.setBounds(10, 32, 66, 16);
		getContentPane().add(portLabel);

		m_portField = new JTextField();
		m_portField.setText("6666");
		m_portField.setBounds(82, 34, 66, 20);
		getContentPane().add(m_portField);

		final JButton connectButton = new JButton();
		connectButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(final ActionEvent arg0)
			{
				Client client = null;
				try
				{
					client = new Client(m_ipField.getText(), Integer.parseInt(m_portField.getText()), m_core);
				}
				catch (Exception e)
				{
					JOptionPane.showMessageDialog(null, "Couldnt connect to host", "Connection Error",
							JOptionPane.OK_OPTION);
					// JOptionPane.showMessageDialog(null,
					// "Please select a Sensor first", "entry check failed",
					// JOptionPane.OK_OPTION);
					return;
				}
				client.start();
				ClientView cv = new ClientView(m_core);
				cv.show();
				dispose();
			}
		});
		connectButton.setText("Connect");
		connectButton.setBounds(226, 67, 106, 26);
		getContentPane().add(connectButton);
		setVisible(true);
		//
	}
}
